<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\User;
use Illuminate\Support\Facades\Schema;
use App\Http\model\Curdpost;
class DriverController extends BaseController
{
    /**
     * 司机信息接口
     * by 吉星
     */
    // 司机信息数据
    public function driver(Request $request){
        // $res = new Curdpost();
        // $data = $res->selectFieldArr("driver");
        // dd($data);exit;
        if($request){
            $where = [];
            // $data = [];
            if($request['driver_name']){
                $where['driver_name'] = $request['driver_name'];
            }
            if($request['b.type']){
                $where['b.type'] = $request['b.type'];
            }
            if ($request['company_name']) {
                $where['b.company_name'] = $request['company_name'];
            }
            if ($request['convoy_name']) {
                $where['c.name'] = $request['convoy_name'];
            }
            if ($request['role']) {
                $where['role'] = $request['role'];
            }
            if ($request['attendant']) {
                $where['attendant'] = $request['attendant'];
            }
            if ($request['account_status']) {
                $where['account_status'] = $request['account_status'];
            }
            if ($request['starttime'] || $request['endtime']) {
                $starttime = strtotime($request['starttime']);
                $endtime = strtotime($request['endtime']);
                $data = Db::table('driver as a')
                    ->where($where)
                    ->select('*','a.id as aid','e.name as seriesname','c.name as cheduiname','u.name as username','a.sex as dsex')
                    ->where('a.is_delete','=','1')
                    ->whereBetween('a.save_time',[$starttime,$endtime])
                    ->join('branch as b','a.contract_company','=','b.id','left')
                    ->join('convoy as c','a.convoy_id','=','c.id','left')
                    ->join('car_car as d','a.car_id','=','d.id','left')
                    ->join('car_type as e','e.id','=','d.typeid','left')
                    ->join('car_series as f','f.id','=','e.sid','left')
                    ->join('user as u','u.id','=','a.save_userid','left')
                    ->get()
                    ->toArray();
            }else{
                $data = Db::table('driver as a')
                    ->where($where)
                    ->select('*','a.id as aid','e.name as seriesname','c.name as cheduiname','u.name as username','a.sex as dsex')
                    ->where('a.is_delete','=','1')
                    ->join('branch as b','a.contract_company','=','b.id','left')
                    ->join('convoy as c','a.convoy_id','=','c.id','left')
                    ->join('car_car as d','a.car_id','=','d.id','left')
                    ->join('car_type as e','e.id','=','d.typeid','left')
                    ->join('car_series as f','f.id','=','e.sid','left')
                    ->join('user as u','u.id','=','a.save_userid','left')
                    ->get()
                    ->toArray();
            }
            $data = json_decode(json_encode($data),true);
            foreach ($data as $k => $v) {
                 if($v['is_zx'] == 1){
                    $data[$k]['is_zx'] = "上线";
                }elseif($v['attendant'] == 5){
                    $data[$k]['renshis'] = "休假中";
                }elseif($v['attendant'] == 6){
                    $data[$k]['renshis'] = "离职";
                }elseif($v['attendant'] == 1){
                    $data[$k]['yunying'] = "载客";
                }elseif($v['attendant'] == 2){
                    $data[$k]['yunying'] = "接单";
                }elseif($v['attendant'] == 3){
                    $data[$k]['yunying'] = "空驶";
                }elseif($v['attendant'] == 4){
                    $data[$k]['yunying'] = "停运";
                }else{
                    $data[$k]['is_zx'] == '下线';
                    $data[$k]['yunying'] == '停运';
                    $data[$k]['renshis'] == '正常';
                }
                switch ($v['account_status']) {
                    case '1':
                        $data[$k]['account_status'] = "正常";
                        break;
                    case '2':
                        $data[$k]['account_status'] = "未审核";
                        break;
                    case '3':
                        $data[$k]['account_status'] = "冻结";
                        break;
                    case '4':
                        $data[$k]['account_status'] = "待激活";
                        break;
                    case '5':
                        $data[$k]['account_status'] = "审核打回";
                        break;
                    default:
                        # code...
                        break;
                }
            }
            if($data){
                $list = ['code'=>1000,'message'=>'获取数据成功!','data'=>$data,'where'=>$where];
            }else{
                $list = ['code'=>1002,'message'=>'没有找到符合条件的数据!','where'=>$where];
            }
        }else{
            $list = ['code'=>1001,'message'=>'参数错误!'];
        }
        $list = json_decode(json_encode($list),true);
        return $list;
    }
    // 修改司机页面赋值
    public function edit(Request $request){
        if($request->all('id')){
            $data = Db::table('driver')
                ->where('id',$request['id'])
                ->first();
            $data = json_decode(json_encode($data),true);
            if($data){
                $res = ['code'=>1000,'message'=>'获取数据成功!','data'=>$data];
            }else{
                $res = ['code'=>1001,'message'=>'获取数据失败,请刷新重试!'];
            }
        }else{
            $res = ['code'=>1002,'message'=>'参数错误!'];
        }
        return $res;
    }
    // 修改司机信息
    public function driver_edit(Request $request){
        if($request){
            $data = $request->all();
            if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }
            if($data['id']){
                $file = $reques['file1'];//获取个人照片
                $file1 = $reques['file2'];//获取个人身份证正面照片
                $file2 = $reques['file3'];//获取健康证照片
                $file3 = $reques['file4'];//获取驾驶证照片
                $file4 = $reques['file5'];//获取个人身份证反面照片
                //return $file;
                if ($file) {
                    $data['user_pic'] = $file;
                }
                if($file1){
                    $data['pin_pic'] = $$file1;
                }
                if($file2){
                    $data['passport_pic'] = $$file2;
                }
                if($file3){
                    $data['licenseId_pic'] = $$file3;
                }
                if($file4){
                    $data['pinf_pic'] = $$file4;
                }
                $data['save_userid'] = session('uid');
                $data['save_time'] = date('Y-m-d H:i;s',time());
                $data['is_delete'] = 1;
                $save = Db::table('driver')->where('id','=',$data['id'])->update($data);
                $list = Db::table('driver')->where('id',$request['id'])->first();
                $list = json_decode(json_encode($list),true);
                if($list['sex']==1){
                    $list['xingbie'] = "男";
                }else{
                    $list['xingbie'] = "女";
                }
                $res = new Curdpost();
                $where = "DriverPhone = ".'"'.$list['driver_phone'].'"';
                $array = ['CompanyId'=>'test','Address'=>110100,'DriverPhone'=>$data['driver_phone'],'DriverName'=>$data['driver_name'],'DriverGender'=>$xingbie,'DriverBirthday'=>substr($data['pin_number'], 6,8),'DriverNation'=>$data['nation'],'DriverContactAddress'=>$data['home_address'],'GetDriverLicenseDate'=>date("Ymd",strtotime($data['frist_time'])),'DriverLicenseOn'=>date("Ymd",strtotime($data['licenseId_firsttime'])),'DriverLicenseOff'=>date("Ymd",strtotime($data['licenseId_endtime'])),'TaxiDriver'=>1,'CertificateNo'=>'暂时为空','NetworkCarlssueOrganization'=>'暂时为空','NetworkCarlssueDate'=>'20180619','GetNetworkCarProofDate'=>'20180619','NetworkCarProofOn'=>'20180619','RegisterDate'=>'20180619','CommercialType'=>1,'ContractCompany'=>'暂时为空','ContractOn'=>'20180619','ContractOff'=>20180619,'State'=>1,'Flag'=>2,'DriverAddress'=>$data['participant'],'EmergencyContact'=>$data['contact'],'EmergencyContactPhone'=>$data['contact_phone'],'Licenseld'=>$data['licenseId'],'UpdateTime'=>date("YmdHis",time())];
                $result = $res->NewUpdate('driver',$array,$where);
                if($save){
                    $res = ['code'=>1000,'message'=>'修改成功!'];
                }else{
                    $res = ['code'=>1001,'message'=>'修改失败,没有数据发生变化!'];
                }
            }else{
                $res = ['code'=>1002,'message'=>'修改失败,没有找到符合条件的数据!'];
            }
        }else{
            $res = ['code'=>1003,'message'=>'参数错误!'];
        }
        return $res;
    }

    // 删除司机 软删除
    public function del(Request $request){
    	if($request['id']){
            $id = explode(',', $request['id']);
    		$data['is_delete'] = 2;
    		$data['save_time'] = date('Y-m-d H:i;s',time());
    		$data['save_userid'] = session('uid');
    		$data['car_id'] = '';
    		$data['account_status'] = 2;
    		$dels = Db::table('driver')->whereIn('id',$id)->update($data);
            // for ($i=0; $i < count($id); $i++) { 
            //     $list = Db::table('driver')->where('id',$id[$i])->first();
            //     $list = json_decode(json_encode($list),true);
            //     $res = new Curdpost();
            //     $where = "DriverPhone = ".'"'.$list['driver_phone'].'"';
            //     $result = $res->NewDel('driver',$where);
            // }
                
            // dump($list);exit;
    		if($dels){
    			$res = ['code'=>1000,'message'=>'删除成功!'];
    		}else{
    			$res = ['code'=>1002,'message'=>'删除失败,请确认该数据是否已经删除!'];
    		}
    	}else{
    		$res = ['code'=>1001,'message'=>'未找到要删除的数据!'];
    	}
    	return $res;
    }

    // 重置密码
    public function repassword(Request $request){
    	if($request['id']){
    		$data['save_time'] = date('Y-m-d H:i;s',time());
    		$data['save_userid'] = session('uid');
    		$data['password'] = "###".md5(md5(trim($request['password'])));
    		$del = Db::table('driver')->where('id',$request['id'])->update($data);
    		if($del){
    			$res = ['code'=>1000,'message'=>'修改密码成功!'];
    		}else{
    			$res = ['code'=>1002,'message'=>'修改密码失败,请确认该数据是否已经修改!'];
    		}
    	}else{
    		$res = ['code'=>1001,'message'=>'未找到要修改的数据!'];
    	}
    	return $res;
    }
    // 冻结司机
    public function dongjie(Request $request){
    	if($request['id']){
    		$data['account_status'] = 3;
    		$data['save_time'] = date('Y-m-d H:i;s',time());
    		$data['save_userid'] = session('uid');
    		$del = Db::table('driver')->whereIn('id',$request['id'])->update($data);
    		if($del){
    			$res = ['code'=>1000,'message'=>'账号冻结成功!'];
    		}else{
    			$res = ['code'=>1002,'message'=>'账号冻结失败,请确认该账号是否已经冻结!'];
    		}
    	}else{
    		$res = ['code'=>1001,'message'=>'未找到要冻结的账号!'];
    	}
    	return $res;
    }
    // 司机解除冻结
    public function jiechudongjie(Request $request){
    	if($request['id']){
    		$data['account_status'] = 1;
    		$data['save_time'] = date('Y-m-d H:i;s',time());
    		$data['save_userid'] = session('uid');
    		$del = Db::table('driver')->whereIn('id',$request['id'])->update($data);
    		if($del){
    			$res = ['code'=>1000,'message'=>'账号解除冻结成功!'];
    		}else{
    			$res = ['code'=>1002,'message'=>'账号解除冻结失败,请确认该账号是否已经解除!'];
    		}
    	}else{
    		$res = ['code'=>1001,'message'=>'未找到要解除冻结的账号!'];
    	}
    	return $res;
    }

    // 关联车辆->车辆列表
    public function carlist(Request $request){
    	// 获得司机所在车队ID
    	$siji = Db::table('driver')->where('id',$request['id'])->first(); //司机信息
    	$siji = json_decode(json_encode($siji),true);
    	$chedui = isset($siji['convoy_id']) ? $siji['convoy_id'] : ''; //车队ID
    	if($chedui){
    		// 根据车队ID获取车队所属公司
    		$company_id = Db::table('convoy')
    			->where('id',$chedui)
    			->first();
    		$company_id = json_decode(json_encode($company_id),true);
    		// dump($company_id);exit;
    		$where = [];
            if(!empty($request['cnum'])){ //车牌号
                $where['c.num'] = $request['cnum'];
            }
            if(!empty($request['bid'])){ //车品牌id
                $where['b.id'] = $request['bid'];
            }
            if (!empty($request['sid'])) { //车系
                $where['s.id'] = $request['sid'];
            }
            if (!empty($request['tid'])) { //车型
                $where['t.id'] = $request['tid'];
            }
            if (!empty($request['imei'])) {
                $where['c.imei'] = $request['imei'];
            }
            // if (isset($request['attendant'])) {
            //     $where['attendant'] = $request['attendant'];
            // }
            // if (isset($request['account_status'])) {
            //     $where['account_status'] = $request['account_status'];
            // }
    		// 根据车队ID获取车队所属车辆
    		if(!empty($request['guanlian']) && $request['guanlian'] == 1){
    			// 查看关联车辆
	    		$carlist = Db::table('car_car as c')
		    		->select('c.num','c.imei','c.color','c.id as cid','t.name as tname','s.name as sname','d.driver_name','d.driver_phone','b.name as bname','d.Address','gs.company_name','cd.name as cdname')
		    		->join('car_type as t','c.typeid','=','t.id','left')
		    		->join('driver as d','d.car_id','=','c.id','left')
		    		->join('car_brand as b','b.id','=','t.brandid','left')
		    		->join('car_series as s','s.id','=','t.sid','left')
                    ->join('branch as gs','gs.id','=','c.teamid','left')
                    ->join('convoy as cd','cd.id','=','d.convoy_id','left')
		    		->where('c.teamid',$company_id['branch_id'])
		    		->where('t.isdel',1)
		    		->where('d.car_id','!=',null)
		    		->where('b.type','=',2)
		    		->where($where)
		    		->get()
		    		->toArray();
	    	}elseif(!empty($request['guanlian']) && $request['guanlian'] == 2){
	    		// 查看未关联车辆
	    		$carlist = Db::table('car_car as c')
		    		->select('c.num','c.imei','c.color','c.id as cid','t.name as tname','s.name as sname','d.driver_name','d.driver_phone','b.name as bname','d.Address','gs.company_name','cd.name as cdname')
		    		->join('car_type as t','c.typeid','=','t.id','left')
		    		->join('driver as d','d.car_id','=','c.id','left')
		    		->join('car_brand as b','b.id','=','t.brandid','left')
		    		->join('car_series as s','s.id','=','t.sid','left')
                    ->join('branch as gs','gs.id','=','c.teamid','left')
                    ->join('convoy as cd','cd.id','=','d.convoy_id','left')
		    		->where('c.teamid',$company_id['branch_id'])
		    		->where('t.isdel',1)
		    		->where('d.car_id','=',null)
		    		->where('b.type','=',2)
		    		->where($where)
		    		->get()
		    		->toArray();
	    	}else{
	    		$carlist = Db::table('car_car as c')
		    		->select('c.num','c.imei','c.color','c.id as cid','t.name as tname','s.name as sname','d.driver_name','d.driver_phone','b.name as bname','d.Address','gs.company_name','cd.name as cdname')
		    		->join('car_type as t','c.typeid','=','t.id','left')
		    		->join('driver as d','d.car_id','=','c.id','left')
		    		->join('car_brand as b','b.id','=','t.brandid','left')
                    ->join('car_series as s','s.id','=','t.sid','left')
                    ->join('branch as gs','gs.id','=','c.teamid','left')
                    ->join('convoy as cd','cd.id','=','d.convoy_id','left')
		    		->where('c.teamid',$company_id['branch_id'])
		    		->where('t.isdel',1)
		    		->where('b.type','=',2)
		    		->where($where)
		    		->get()
		    		->toArray();
	    	}
	    	$carlist = json_decode(json_encode($carlist),true);
            foreach ($carlist as $key => $value) {
                $carlist[$key]['chexing'] = $value['bname'].$value['sname'].$value['tname'];
                if(!empty($value['Address'])){
                    // foreach ($carlist as $k => $v) {
                    $areaObj = Db::table('region')
                            ->where('code',$value['Address'])
                            ->first();
                    $areaArr = json_decode(json_encode($areaObj),true);
                    $parentname = Db::table("region")
                        ->where('code',$areaArr['parentCode'])
                        ->first();
                    $parentname = json_decode(json_encode($parentname),true);
                    $carlist[$key]['fullName'] = $parentname['fullName'].'/'.$areaArr['fullName'];
                    // }
                }
                $carlist[$key]['xinxi'] = "地区:".$carlist[$key]['fullName']."<br>"."公司:".$carlist[$key]['company_name'].'<br>'."车队:".$carlist[$key]['cdname'].'';
                $carlist[$key]['siji'] = $carlist[$key]['driver_name'].'<br>'.$carlist[$key]['driver_phone'];
            }
	    	if($carlist){
	    		$carlist = ['code'=>1000,'message'=>'查询数据成功!','data'=>$carlist];
	    	}else{
	    		$carlist = ['code'=>1002,'message'=>'没有查找到车辆信息!'];
	    	}
    	}else{
    		$carlist = ['code'=>1001,'message'=>'该司机没有车队!'];
    	}
    	// 获取品牌列表
    	$pinpai = Db::table('car_brand')->where('type','=',1)->get()->toArray();
    	$pinpai = json_decode(json_encode($pinpai),true);
    	// 获取子品牌列表
    	$zipinpai = Db::table('car_brand')->where('type','=',2)->get()->toArray();
    	$zipinpai = json_decode(json_encode($zipinpai),true);
    	// 获取车系列表
    	$chexi = Db::table('car_series')->where('isdel','=',1)->get()->toArray();
    	$chexi = json_decode(json_encode($chexi),true);
    	// 获取车型列表
    	$chexing = Db::table('car_type')->where('isdel','=',1)->get()->toArray();
    	$chexing = json_decode(json_encode($chexing),true);
    	return ['carlist'=>$carlist,'pinpai'=>$pinpai,'zipinpai'=>$zipinpai,'chexi'=>$chexi,'chexing'=>$chexing];
    }
    // 司机绑定车辆
    public function bdcar(Request $request){
    	if($request['car_id'] && $request['driver_id']){
    		$data['car_id'] = $request['car_id'];
    		$data['save_time'] = date('Y-m-d H:i;s',time());
    		$data['save_userid'] = session('uid');
    		// 查看该车辆是否配备司机
    		$driver = Db::table('driver')->where('car_id',$request['car_id'])->first();
    		$driver = json_decode(json_encode($driver),true);
    		if($driver){
    			// 通过车辆ID关联订单表,查看该车辆是否有未完成订单
	    		$order = Db::table('order as o')
	    			->join('order_extend as e','o.extendid','=','e.id')
	    			->where('e.driver_id',$driver['id'])
	    			->whereBetween('o.implementstatus',[3,8])
	    			->first();
	    		$order = json_decode(json_encode($order),true);
	    		if($order){
	    			$res = ['code'=>1001,'message'=>'车辆换绑失败,该车辆有未完成的订单,暂时无法换绑!'];
	    		}else{
	    			$list = Db::table('driver')
	    			->where('id',$request['driver_id'])
	    			->update($data);
	    			if($list){
	    				$res = ['code'=>1000,'message'=>'车辆换绑成功!'];
	    			}else{
	    				$res = ['code'=>1003,'message'=>'换绑失败,请确认该车辆是否已经绑定给该司机!'];
	    			}
	    		}
    		}else{
    			$list = Db::table('driver')
    			->where('id',$request['driver_id'])
    			->update($data);
    			if($list){
    				$res = ['code'=>1000,'message'=>'车辆关联成功!'];
    			}else{
    				$res = ['code'=>1002,'message'=>'关联失败,请确认该车辆是否已经关联给该司机!'];
    			}
    		}
    	}else{
    		$res = ['code'=>1001,'message'=>'没有找到符合条件的数据!'];
    	}
        return $res;
    }

    // 添加司机
    public function add(Request $request){
    	if($request){
    		$data = $request->all();
            if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }
    		$file = $request['file1'];//获取个人照片
    		$file1 = $request['file2'];//获取个人身份证照片正面
    		$file2 = $request['file3'];//获取健康证照片
            $file3 = $request['file4'];//获取驾驶证照片
    		$file4 = $request['file5'];//获取个人身份证照片反面
            //return $file;
            if ($file) {
                $data['user_pic'] = $file;
            }
            if($file1){
                $data['pin_pic'] = $file1;
        	}
            if($file2){
                $data['passport_pic'] = $file2;
        	}
            if($file3){
                $data['licenseId_pic'] = $file3;
        	}
            
            if($file4){
                $data['pinf_pic'] = $file4;
            }
    		$data['save_userid'] = session('uid');
			$data['save_time'] = date('Y-m-d H:i;s',time());
			$data['is_delete'] = 1;
			$data['role'] = 1;
			$data['account_status'] = 4;
    		$add = Db::table('driver')->insert($data);
    		if($add){
    			$res = ['code'=>1000,'message'=>'司机增加成功!'];
    		}else{
    			$res = ['code'=>1001,'message'=>'司机增加失败,请刷新后重试!'];
    		}
    	}else{
    		$res = ['code'=>1003,'message'=>'参数错误!'];
    	}
    	return $res;
    }
    // 司机详情页信息
    public function driveritems(Request $request){
        if($request->all('id')){
            // 司机信息
            $driver = Db::table('driver')
                ->where('id','=',$request['id'])
                ->first();
            $driver = json_decode(json_encode($driver),true);
            // 关联车辆信息
            if($driver['car_id']){
                $car = Db::table('car_car as c')
                    ->join('car_type as t','c.typeid','=','t.id')
                    ->where('t.isdel',1)
                    ->select('c.num','t.name','c.modifier','c.modtime')
                    ->first();
                $car = json_decode(json_encode($car),true);
            }else{
                $car = [];
            }
            //信誉信息
            $xinyu = Db::table('credibility')->where('driver_id','=',$request['id'])->first();
            $xinyu = json_decode(json_encode($xinyu),true);
            $res = ['code'=>1000,'message'=>'获取数据成功'];
        }else{
            $res = ['code'=>1001,'message'=>'参数错误'];
        }
        return ['res'=>$res,'car'=>$car,'xinyu'=>$xinyu,'data'=>$driver];
    }
    // 司机详情页订单列表
    public function driverorder(Request $request){
        if($request->all('id')){
            $order = Db::table('order as o')
                ->join('order_extend as e','o.extendid','=','e.id')
                ->where('e.driver_id','=',$request['id'])
                ->paginate(10);
            $page = $order->links();
            $order = json_decode(json_encode($order),true);
            $res = ['code'=>1000,'message'=>'获取数据成功!','data'=>$order,'page'=>$page];
        }else{
            $res = ['code'=>1001,'message'=>'获取数据失败,没有找到符合条件的数据!'];
        }
        return $res;
    }

    // 司机处罚管理
    public function chufa(Request $request){
        if($request){
            $where = [];
            if($request['driver_name']){
                $where['b.driver_name'] = $request['driver_name'];
            }
            if($request['type']){
                $where['d.type'] = $request['type'];
            }
            if ($request['company_name']) {
                $where['d.company_name'] = $request['company_name'];
            }
            if ($request['convoy_name']) {
                $where['e.name'] = $request['convoy_name'];
            }
            if ($request['role']) {
                $where['b.role'] = $request['role'];
            }
            if ($request['attendant']) {
                $where['b.attendant'] = $request['attendant'];
            }
            if ($request['account_status']) {
                $where['b.account_status'] = $request['account_status'];
            }
            if ($request['starttime'] || $request['endtime']) {
                $starttime = strtotime($request['starttime']);
                $endtime = strtotime($request['endtime']);
                $data = Db::table('disciplined as a')
                    ->join('driver as b','b.id','=','a.driver_id')
                    ->join('car_car as c','c.id','=','b.car_id')
                    ->join('branch as d','d.id','=','c.teamid')
                    ->join('convoy as e','e.id','=','b.convoy_id')
                    ->join('car_type as f','f.id','=','c.typeid')
                    ->join('car_series as g','g.id','=','f.sid')
                    ->select('*','a.id as aid')
                    ->where($where)
                    ->whereBetween('a.update_time',[$starttime,$endtime])
                    ->paginate(5);
                $page = $order->links();
                $data = json_decode(json_encode($data),true);
            }else{
                $data = Db::table('disciplined as a')
                    ->join('driver as b','b.id','=','a.driver_id')
                    ->join('car_car as c','c.id','=','b.car_id')
                    ->join('branch as d','d.id','=','c.teamid')
                    ->join('convoy as e','e.id','=','b.convoy_id')
                    ->join('car_type as f','f.id','=','c.typeid')
                    ->join('car_series as g','g.id','=','f.sid')
                    ->select('*','a.id as aid')
                    ->where($where)
                    ->paginate(5);
                $page = $order->links();
                $data = json_decode(json_encode($data),true);
            }
            if($data){
                $res = ['code'=>1000,'message'=>'获取数据成功!','data'=>$data,'page'=>$page,'where'=>$where];
            }else{
                $res = ['code'=>1001,'message'=>'没有找到符合条件的数据','data'=>$data,'page'=>$page,'where'=>$where];
            }
        }else{
            $res = ['code'=>1002,'message'=>'参数错误!'];
        }
        return $res;
    }
    // 处罚页面司机列表搜索查询选项
    public function chafaaddoption(Request $request){
        // 地区选项
        $diqu = Db::table('branch')->select('area')->groupBy('area')->get()->toArray();
        $diqu = json_decode(json_encode($diqu),true);
        // 公司选项
        $branch = Db::table('branch')->select('id','company_name')->get()->toArray();
        $branch = json_decode(json_encode($branch),true);
        // 车队选项
        $convoy = Db::table('convoy')->select('id','name')->get()->toArray();
        $convoy = json_decode(json_encode($convoy),true);
        $res = ['code'=>1000,'message'=>'获取数据成功','diqu'=>$diqu,'branch'=>$branch,'convoy'=>$convoy];
        return $res;
    }
    // 添加处罚页面司机列表
    public function chufaaddlist(Request $request){
        if($request){
            $where = [];
            if($request['driver_name']){
                $where['a.driver_name'] = $request['driver_name'];
            }
            if($request['driver_phone']){
                $where['a.driver_phone'] = $request['driver_phone'];
            }
            if($request['area']){
                $where['b.area'] = $request['area'];
            }
            if($request['company_name']){
                $where['b.company_name'] = $request['company_name'];
            }
            if($request['name']){
                $where['c.name'] = $request['name'];
            }
            $list = Db::table('driver as a')
                ->join('convoy as c','a.convoy_id','=','c.id')
                ->join('branch as b','c.branch_id','=','b.id')
                ->where($where)
                ->get()
                ->toArray();
            $list = json_decode(json_encode($list),true);
            if($list){
                $res = ['code'=>1000,'message'=>'获取数据成功!','data'=>$list,'where'=>$where];
            }else{
                $res = ['code'=>1001,'message'=>'没有找到符合条件的数据!','data'=>$list,'where'=>$where];
            }
        }else{
            $res = ['code'=>1002,'message'=>'参数错误!'];
        }
        return $res;
    }
    // 添加处罚
    public function chufainsert(Request $request){
        if($request){
            $data = $request->all();
            if (isset($data['s'])) {
                $data = array_except($data, ['s']);
            }
            $add = Db::table('disciplined')->insert($data);
            if($add){
                $res = ['code'=>1000,'message'=>'添加成功!'];
            }else{
                $res = ['code'=>1001,'message'=>'添加失败!'];
            }
        }else{
            $res = ['code'=>1002,'message'=>'参数错误!'];
        }
        return $res;
    }
    // 删除处罚信息
    public function chufadel(Request $request){
        if($request['id']){
            $del = Db::table('disciplined')
                ->where('id','=',$request['id'])
                ->delete();
            if($del){
                $res = ['code'=>1000,'message'=>'删除成功'];
            }else{
                $res = ['code'=>1001,'message'=>'删除失败,请刷新后重试!'];
            }
        }else{
            $res = ['code'=>1002,'message'=>'参数错误'];
        }
        return $res;
    }
}
